package com.wephone.Student.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.wephone.Student.R;
import com.wephone.Student.bean.data.CityData;
import com.wephone.Student.bean.data.CountryData;
import com.wephone.Student.bean.data.ProvinceData;
import com.wephone.Student.wheel.widget.OnWheelChangedListener;
import com.wephone.Student.wheel.widget.StringWheelAdapter;
import com.wephone.Student.wheel.widget.WheelView;

public class HouseInfoSearchActivity extends BaseActivity {
	private RadioButton houseTypeRadio1;
	private RadioButton houseTypeRadio2;
	private RadioButton houseTypeRadio3;
	private RadioButton houseSourceRadio1;
	private RadioButton houseSourceRadio2;
	private EditText searchEdit;
	private TextView inputEdit0;
	private TextView inputEdit1;
	private EditText inputEdit2;
	private int nCountryIdx;
	private int nProvinceIdx;
	private int nCityIdx;
	private String searchDefaultCountry;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_house_info_search);
		
		init();
	}
	
	private void init(){
		searchEdit = (EditText)findViewById(R.id.search_edit);
		inputEdit0 = (TextView)findViewById(R.id.input_edit_0);
		inputEdit1 = (TextView)findViewById(R.id.input_edit_1);
		inputEdit2 = (EditText)findViewById(R.id.input_edit_2);
		houseTypeRadio1 = (RadioButton)findViewById(R.id.house_type_radio_1);
		houseTypeRadio2 = (RadioButton)findViewById(R.id.house_type_radio_2);
		houseTypeRadio3 = (RadioButton)findViewById(R.id.house_type_radio_3);
		houseSourceRadio1 = (RadioButton)findViewById(R.id.house_source_radio_1);
		houseSourceRadio2 = (RadioButton)findViewById(R.id.house_source_radio_2);
		
		inputEdit0.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				showChooseCountryDialog("国家",inputEdit0,inputEdit1);
			}
		});
		
		inputEdit1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(TextUtils.isEmpty(inputEdit0.getText().toString())){
					Toast.makeText(getApplicationContext(), "请先选择国家", Toast.LENGTH_SHORT).show();
				}else{
					showChooseCountryDialog2(getString(R.string.house_info_search_item_3),inputEdit1);
				}
			}
		});
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		findViewById(R.id.btn_search).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				if(!TextUtils.isEmpty(searchEdit.getText().toString().trim())){
					intent.putExtra("hTitle", searchEdit.getText().toString().trim());
				}
				if(!TextUtils.isEmpty(inputEdit0.getText().toString())){
					intent.putExtra("hLoc", inputEdit0.getText().toString()+" "+inputEdit1.getText().toString());
				}
				if(!TextUtils.isEmpty(inputEdit2.getText().toString())){
					intent.putExtra("hHouseType", inputEdit2.getText().toString());
				}
				if(houseTypeRadio1.isChecked()){
					intent.putExtra("hSaleType", "0");
				}else if(houseTypeRadio2.isChecked()){
					intent.putExtra("hSaleType", "1");
				}else if(houseTypeRadio3.isChecked()){
					intent.putExtra("hSaleType", "2");
				}
				if(houseSourceRadio1.isChecked()){
					intent.putExtra("hSaleSource", "0");
				}else if(houseSourceRadio2.isChecked()){
					intent.putExtra("hSaleSource", "1");
				}
				setResult(111,intent);
				finish();
			}
		});
		
		Bundle bundle = getIntent().getExtras();
		if(bundle!=null){
			String searchDefaultCountry = bundle.getString("searchDefaultCountry");
			if(!TextUtils.isEmpty(searchDefaultCountry)){
				for(int i=0;i<BaseActivity.getCountryList(getApplicationContext()).size();i++){
					if(searchDefaultCountry.equals(BaseActivity.getCountryList(getApplicationContext()).get(i).getN())){
						nCountryIdx = i;
						break;
					}
				}
				inputEdit0.setText(searchDefaultCountry);
			}
		}
	}
	
	private void showChooseCountryDialog(String title,final TextView inputEdit1,final TextView inputEdit2){
		View inputView = getLayoutInflater().inflate(R.layout.dialog_wheel_1_view, null);
		final WheelView wheel = (WheelView)inputView.findViewById(R.id.wheel_1);
		List<String> countryList = new ArrayList<String>();
		for(CountryData cd:BaseActivity.getCountryList(getApplicationContext())){
			countryList.add(cd.getN());
		}
        wheel.setAdapter(new StringWheelAdapter(countryList));
        wheel.setCurrentItem(nCountryIdx);
        wheel.setCyclic(true);
        wheel.setInterpolator(new AnticipateOvershootInterpolator());
		AlertDialog.Builder builder = new AlertDialog.Builder(HouseInfoSearchActivity.this);
		builder.setTitle(title);
		builder.setView(inputView);  
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){  
			public void onClick(DialogInterface dialog, int whichButton){
				nProvinceIdx = 0;
				nCityIdx = 0;
				inputEdit2.setText("");
				nCountryIdx = wheel.getCurrentItem();
				inputEdit1.setText(BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getN());
			}
		});  
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){  
			public void onClick(DialogInterface dialog, int whichButton){}
		}); 
		builder.show();  
	}
	
	private void showChooseCountryDialog2(String title,final TextView inputEdit2){
		View inputView = getLayoutInflater().inflate(R.layout.dialog_wheel_2_view, null);
		final WheelView wheel1 = (WheelView)inputView.findViewById(R.id.wheel_1);
		final WheelView wheel2 = (WheelView)inputView.findViewById(R.id.wheel_2);
		
		List<String> provinceList = new ArrayList<String>();
		for(ProvinceData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP()){
			provinceList.add(cd.getN());
		}
        wheel1.setAdapter(new StringWheelAdapter(provinceList));
        wheel1.setCurrentItem(nProvinceIdx);
        wheel1.setCyclic(true);
        wheel1.setInterpolator(new AnticipateOvershootInterpolator());
        wheel1.addChangingListener(new OnWheelChangedListener(){
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				nProvinceIdx = newValue;
				nCityIdx = 0;
				
				List<String> cityList = new ArrayList<String>();
				for(CityData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC()){
					cityList.add(cd.getN());
				}
				wheel2.setAdapter(new StringWheelAdapter(cityList));
				wheel2.setCurrentItem(nCityIdx);
			}});
        
		List<String> cityList = new ArrayList<String>();
		for(CityData cd:BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC()){
			cityList.add(cd.getN());
		}
        wheel2.setAdapter(new StringWheelAdapter(cityList));
        wheel2.setCurrentItem(nCityIdx);
        wheel2.setCyclic(true);
        wheel2.setInterpolator(new AnticipateOvershootInterpolator());
        
		AlertDialog.Builder builder = new AlertDialog.Builder(HouseInfoSearchActivity.this);
		builder.setTitle(title);
		builder.setView(inputView);  
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){  
			public void onClick(DialogInterface dialog, int whichButton){
				nProvinceIdx = wheel1.getCurrentItem();
				nCityIdx = wheel2.getCurrentItem();
				if(TextUtils.isEmpty(BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getN())){
					if(TextUtils.isEmpty(BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC().get(nCityIdx).getN())){
						inputEdit2.setText(BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getN());
					}else{
						inputEdit2.setText(BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC().get(nCityIdx).getN());
					}
				}else{
					inputEdit2.setText(BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getN()+" "+BaseActivity.getCountryList(getApplicationContext()).get(nCountryIdx).getP().get(nProvinceIdx).getC().get(nCityIdx).getN());
				}
			}
		});  
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){  
			public void onClick(DialogInterface dialog, int whichButton){}
		}); 
		builder.setNeutralButton("清空", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				inputEdit2.setText("");
			}
		});
		builder.show();  
	}
	
}
